/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.h5.controller.member;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.cmpt.annotation.Auth;
import com.itttq.gshop.generic.connector.IOrderInfoConnector;
import com.itttq.gshop.generic.connector.IUserInfoConnector;
import com.itttq.gshop.generic.controller.GenericController;

@Auth
@RestController
@RequestMapping("member")
public class MemberController extends GenericController {

	private static final long serialVersionUID = 997162541056404387L;
	@Autowired
	private IUserInfoConnector userInfoConnector;
	@Autowired
	private IOrderInfoConnector orderInfoConnector;
	@InitBinder
	// 解决日前格式校验不通过的问题
	public void initBinder(WebDataBinder webDataBinder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    dateFormat.setLenient(false);
	    webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 会员列表页
	 * 
	 * @param userInfo
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("members.html")
	public ModelAndView getUsers(HttpServletRequest request) {
		return new ModelAndView("/member/members");
	}

	/**
	 * 获取会员信息。
	 * 
	 * @param word	搜索条件
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("memberContent.html")
	public ModelAndView getUsers(UserInfoPo user, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/member/members-content");
		this.getPpt(request, user);
		mv.addObject("members", userInfoConnector.getUsers(user));
		return mv;
	}
	
	/**
	 * 保存UserInfoPo对象，此处只能更新
	 * 
	 * @param userInfo
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ResultTo saveUserInfo(UserInfoPo userInfo, HttpServletRequest request) {
		ResultTo rt = new ResultTo();
		if(null == userInfo.getId()) {
			rt.setCode(ResultTo.PARAM_INCORRECT);
			return rt;
		}
		if(userInfoConnector.saveUserInfo(userInfo) < 1) {
			rt.setCode(ResultTo.BUSINESS_ERR);
		}
		return rt;
	}
	
}